import * as path from "node:path";
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],

  /**
   * 解析配置项
   */
  resolve: {
    /**
     * 路径别名
     */
    alias: {
      // "@": path.resolve(__dirname, "./src"),
      "@": fileURLToPath(new URL("./src", import.meta.url)),
      utils: fileURLToPath(new URL("./src/utils", import.meta.url)),
    },
  },

  server: {
    /**
     * 端口号
     *    默认值是 5173
     */
    // port: 9090,

    /**
     * 服务代理，处理跨域问题
     * PS: 只在本地开发生效
     */
    proxy: {
      /**
       * :5173/abcd         不能应该该规则
       * :5173/api          => :8001/api
       * :5173/api/hello    => :8001/api/hello
       */
      "/api": {
        // 目标服务
        target: "http://127.0.0.1:8001",
        /**
         * 路径重写
         * :5173/abcd         不能应该该规则
         * :5173/api          => :8001
         * :5173/api/hello    => :8001/hello
         */
        rewrite: (path) => path.replace("/api", ""),
      },
    },
  },
});
